﻿@using General.Entities.Models
@{
    Layout = null;
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
    var typeList = (List<tc_sysconftype>)ViewBag.typeList;
    var configList = (List<tc_sysconfig>)ViewBag.configList;
}

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-sm12 layui-col-md12">
            <div class="layui-card">
                <!-- 选项卡开始 --> 
                <div class="layui-tab layui-tab-brief">
                    <ul class="layui-tab-title">
                        @for (int i = 0; i < typeList.Count; i++)
                        {
                            <li class="@Html.Raw(i==0?"layui-this":"")">@typeList[i].name</li>
                        }
                        <li class="addConfigBtn"><a class="layui-btn layui-btn-xs" style="margin-top:10px">添加设置</a></li>
                    </ul>
                    <div class="layui-tab-content">
                        @for (int i = 0; i < typeList.Count; i++)
                        {
                            <!-- tab -->
                            <div class="layui-tab-item @Html.Raw(i==0?"layui-show":"")">
                                <form class="layui-form" id="@Html.Raw("configInfoForm"+i)" lay-filter="@Html.Raw("configInfoForm"+i)" style="max-width: 700px;padding: 25px 10px 0 0;">

                                    @foreach (var item in configList)
                                    {
                                        if (item.tid != typeList[i].id)
                                        {
                                            continue;
                                        }
                                    <div class="layui-form-item">
                                        <label class="layui-form-label @Html.Raw(item.required==1?"layui-form-required":"")">@item.title:</label>
                                        <div class="layui-input-block">
                                            @if (item.type == 1)
                                            {
                                                <input name="@item.code" value="@item.value" class="layui-input"
                                                       @Html.Raw(item.required == 1 ? "lay-verify='required' required " : "") />
                                            }
                                            else if (item.type == 2)
                                            {
                                                <textarea name="@item.code" placeholder="" class="layui-textarea" @Html.Raw(item.required == 1 ? "lay-verify='required' required " : "")>@item.value</textarea>
                                            }
                                            else if (item.type == 3)
                                            {
                                                <input type="radio" name="@item.code" value="1" title="是" @Html.Raw(item.value == "1" ? "checked" : "") />
                                                <input type="radio" name="@item.code" value="0" title="否" @Html.Raw(item.value == "0" ? "checked" : "") />
                                            }
                                            else if (item.type == 4)
                                            {
                                                <input type="text" name="@item.code" value="@item.value" placeholder="" disabled class="layui-input inline-block"  style="width: 160px;" @Html.Raw(item.required == 1 ? "lay-verify='required' required " : "") />
                                                <button type="button" class="layui-btn layui-btn-primary @Html.Raw("choosePic"+item.code+"Btn")">选择图片</button>
                                            }


                                            @if (!string.IsNullOrEmpty(item.describe))
                                            {
                                                <div class="layui-word-aux">@item.describe</div>
                                            }
                                        </div>
                                        @if (item.type == 4)
                                        {
                                            <div class="layui-input-block">
                                                <img src="@item.value" class="@Html.Raw("showPic"+item.code)" width="60" height="60" />
                                            </div>
                                        }
                                    </div>
                                    }


                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button class="layui-btn" lay-filter="@Html.Raw("configInfo"+typeList[i].id+"Submit")" lay-submit="">
                                                保存设置
                                            </button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        }

                       
                    </div>
                </div>
                <!-- //选项卡结束 -->
            </div>
        </div>
    </div>
</div>


<!--添加设置弹窗-->
<script type="text/html" id="dialogAddConfigDialog">
    <form id="dialogAddConfigForm" lay-filter="dialogAddConfigForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">设置分类:</label>
            <div class="layui-input-block">
                <select name="tid" lay-verify="required" lay-verType="tips" required>
                    <option value="">请选择</option>
                    @foreach (var item in typeList)
                    {
                        <option value="@item.id">@item.name</option>
                    }
                </select>
                <div class="layui-word-aux">配置分组 用于批量设置 不分组则不会显示在系统设置中</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">配置类别:</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="required" lay-verType="tips" required>
                    <option value="">请选择</option>
                    <option value="1">字符</option>
                    <option value="2">文本</option>
                    <option value="3">开关</option>
                    <option value="4">图片</option>
                </select>
                <div class="layui-word-aux">系统会根据不同类型解析配置值</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">配置标题:</label>
            <div class="layui-input-block">
                <input name="title" placeholder="请输入配置标题" class="layui-input"
                       lay-verify="required" lay-verType="tips" required />
                <div class="layui-word-aux">用于后台显示的配置标题</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">配置标识:</label>
            <div class="layui-input-block">
                <input name="code" placeholder="请输入配置标识" class="layui-input"
                       lay-verify="required" lay-verType="tips" required />
                <div class="layui-word-aux">用于函数调用，只能使用英文且不能重复</div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">配置描述:</label>
            <div class="layui-input-block">
                <input name="describe" placeholder="请输入配置描述" class="layui-input" />
                <div class="layui-word-aux">用于对配置的说明</div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">配置值:</label>
            <div class="layui-input-block">
                <input name="value" placeholder="请输入配置值" class="layui-input" />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">是否必填:</label>
            <div class="layui-input-block">
                <input type="radio" name="required" value="1" title="是" checked>
                <input type="radio" name="required" value="0" title="否">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序值:</label>
            <div class="layui-input-block">
                <input name="sort" type="number" placeholder="请输入排序值" class="layui-input" value="100" lay-verify="required" lay-verType="tips" required />
                <div class="layui-word-aux">用于分组显示的顺序</div>
            </div>
        </div>


        <div class="layui-form-item text-right ">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="dialogAddConfigSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>


<script>layui.use(['layer', 'form', 'table', 'util', 'admin', 'zTree', 'dropdown', 'notice', 'element', 'fileChoose'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var util = layui.util;
    var admin = layui.admin;
    var notice = layui.notice;
    var element = layui.element;
    var fileChoose = layui.fileChoose;

    $('.addConfigBtn').click(function () {
        admin.open({
            type: 1,
            title: '添加配置信息',
            area: ['640px', '700px'],
            content: $('#dialogAddConfigDialog').html(),
            success: function (layero, dIndex) {
                form.render();
                // 表单提交事件
                form.on('submit(dialogAddConfigSubmit)', function (data) {
                    notice.msg('处理中', { icon: 4, close: true });
                    $.post("/api/admin/AddSystemConfig", data.field, function (res) {
                        res = JSON.parse(res);
                        notice.destroy();
                        if (res.code == 1) {
                            layer.close(dIndex);
                            notice.msg(res.message, { icon: 1 });
                        } else {
                            notice.msg(res.message, { icon: 2 });
                        }
                    });
                    return false;
                });
            }
        });
    });


    /* 图片上传 */
    @foreach (var item in configList)
    {
        if (item.type == 4)
        {
            @Html.Raw(@"
                $('.choosePic"+ item.code + @"Btn').click(function(){
                        fileChoose.open({
                            fileUrl: '',
                            listUrl: '/api/admin/getfile',
                            num: 1,
                            dialog: {
                                offset: '60px'
                            },
                            upload: {
                                data: {
                                    dir: function () {
                                        return '/';
                                    }
                                },
                                url: '/api/admin/UploadFile2'
                            },
                            onChoose: function (urls) {
                                $('input[name="+"\""+ item.code + "\""+ @"]').val(urls[0].url);
                                $('.showPic" + item.code + @"').attr('src', urls[0].url);
                        }
                    });
                });
            ");
        }
    }



    /* 监听表单提交 */
    @foreach (var item in typeList)
    {
        @Html.Raw(@"
            form.on('submit(configInfo"+ item.id + @"Submit)', function (data) {
                notice.msg('处理中', { icon: 4, close: true });
                var requstData = [];
                for (var key in data.field) {
                    requstData.push({
                        key: key,
                        value: data.field[key]
                    });
                }
                $.post('/api/admin/EditSystemConfig', { tid: "+ item.id + @", data: JSON.stringify(requstData)}, function (res) {
                    res = JSON.parse(res);
                    notice.destroy();
                    if (res.code == 1) {
                        notice.msg(res.message, { icon: 1 });
                    } else {
                        notice.msg(res.message, { icon: 2 });
                    }
                })
                    return false;
            });
        ");
    }

    });</script>